﻿using UnityEditor;
using UnityEngine;
using System.Collections;

//------------------------------------------------------------
// shaco Framework
// Copyright © 2017-2020 chang.liu All rights reserved.
// Feedback: 449612236@qq.com
//------------------------------------------------------------

namespace shaco.Instance.RenameFormat
{
	public class RenameFormatHierarchy : EditorWindow
    {
        [MenuItem("GameObject/shaco/RenameFormat", false, 3)]
		static void RenameBySequeue()
		{
			if (null == Selection.gameObjects || Selection.gameObjects.Length == 0)
				return;

			foreach (var iter in Selection.gameObjects)
			{
				RenameFormatOnce(iter);
			}
		}

		static private void RenameFormatOnce(GameObject target)
		{
            var parentTmp = target.transform;
            if (0 == parentTmp.childCount)
            {
                return;
            }
            string firstNameTmp = parentTmp.GetChild(0).name;
            if (firstNameTmp.Length == 0)
            {
                Debug.LogError("select active GameObject name is empty, parentTmp=" + parentTmp, parentTmp);
                return;
            }

            var baseNameTmp = GetBaseNameWithoutNumber(firstNameTmp);
            var startIndex = char.IsNumber(firstNameTmp[0]) ? firstNameTmp[0] - '0' : 1;
            for (int i = 0; i < parentTmp.childCount; ++i)
            {
                var childTmp = parentTmp.GetChild(i);
                Undo.RecordObject(childTmp.gameObject, "RenameFormat");
                childTmp.name = baseNameTmp + (startIndex + i);
            }

            RenameFormatHierarchy.SetDirty(parentTmp);
		}

		static private string GetBaseNameWithoutNumber(string name)
		{
			string retValue = name;
			for (int i = retValue.Length - 1; i >= 0; --i)
			{
				if (char.IsNumber(retValue[i]))
				{
					retValue = retValue.Remove(i);
				}
				else 
				{
					break;
				}
			}
			return retValue;
		}

		static public void SetDirty(Object target)
        {
            if (target != null)
                EditorUtility.SetDirty(target);

#if UNITY_2018_1_OR_NEWER
            GameObject gameObjectTarget = null;
            if (target is Component)
                gameObjectTarget = ((Component)target).gameObject;
            else if (target is GameObject)
                gameObjectTarget = target as GameObject;
            if (null != gameObjectTarget)
            {
                var prefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetPrefabStage(gameObjectTarget);
                if (null != prefabStage)
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(prefabStage.scene);
                }
                else
                {
                    if (!Application.isPlaying)
                        UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
                }
            }
#elif UNITY_5_3_OR_NEWER
            if (!Application.isPlaying)
                UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
#else
            Debug.LogError("EditorSceneManager+Compatibility SetDirty error: no support method");
#endif
        }
    }
}